package com.yfp.operation.service.impl;

import com.yfp.operation.domain.MachineDO;
import com.yfp.operation.domain.MachineNetWarnDO;
import com.yfp.operation.service.MachineNetWarnService;
import com.yfp.operation.service.MachineService;
import com.yfp.operation.service.MachineWarnEndTimingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @version 1.0
 * @date 2024-05-20 12:05
 */
@Service
@Slf4j
public class MachineWarnEndTimingServiceImpl implements MachineWarnEndTimingService {

    @Resource
    private MachineService machineService;
    @Resource
    private MachineNetWarnService machineNetWarnService;

    @Transactional
    @Override
    public void netWarnEndTiming() {
        // 获取当前时间所有掉线的设备
        List<MachineDO> machineDOList = machineService.getDisconnection();
        //转成map
        Map<String, MachineDO> machineDOMap = machineDOList.stream().collect(Collectors.toMap(MachineDO::getMachineId, Function.identity()));

        // 获取未修复的网络报警表数据
        List<MachineNetWarnDO> machineNetWarnDOList = machineNetWarnService.getAll();
        Map<String, MachineNetWarnDO> netWarnDOMap = new HashMap<>();
        if (!machineNetWarnDOList.isEmpty()) {
            // 转成map
            Map<String, MachineNetWarnDO> map = machineNetWarnDOList.stream().collect(Collectors.toMap(MachineNetWarnDO::getMachineId, Function.identity()));
            netWarnDOMap.putAll(map);
        }

        // 获取所有设备的已经恢复的网络报警数据
        List<MachineNetWarnDO> oneList = machineNetWarnService.getLimitOne();
        Map<String, MachineNetWarnDO> warnDOMap = new HashMap<>();
        if (!oneList.isEmpty()) {
            Map<String, MachineNetWarnDO> map = oneList.stream().collect(Collectors.toMap(MachineNetWarnDO::getMachineId, Function.identity()));
            warnDOMap.putAll(map);
        }

        ArrayList<MachineNetWarnDO> machineNetWarnDOS = new ArrayList<>();

        if (!machineDOMap.isEmpty()) {
            machineDOMap.keySet().forEach(key -> {
                MachineDO machineDO = machineDOMap.get(key);
                String offlineUpdate = machineDO.getOfflineUpdateTime().toString().substring(0, 19);
                String warnId = UUID.randomUUID().toString();
                // 是否包含该key  设备id  如果包含意味着该设备并未恢复过，不需要记录
                if (!netWarnDOMap.containsKey(key)) {
//                    String offlineTime = netWarnDOMap.get(key).getOfflineTime();
//                    if (!offlineTime.equals(offlineUpdate)) {
//                        // 创建网络报警
//                        MachineNetWarnDO netWarnDO = new MachineNetWarnDO();
//                        netWarnDO.setMachineId(machineDO.getMachineId());
//                        netWarnDO.setOfflineTime(offlineUpdate);
//                        netWarnDO.setClearFlag(0);
//                        netWarnDO.setCreatTime(new Date());
//                        netWarnDO.setWarnId(warnId);
//                        if (warnDOMap.containsKey(key)) {
//                            String shijian = shijian(warnDOMap, key, offlineUpdate);
//                            if (shijian != null) {
//                                netWarnDO.setOnlineDuration(shijian);
//                            }
//                        }
//                        machineNetWarnDOS.add(netWarnDO);
//                    }


                    // 创建网络报警
                    MachineNetWarnDO netWarnDO = new MachineNetWarnDO();
                    netWarnDO.setMachineId(machineDO.getMachineId());
                    netWarnDO.setOfflineTime(offlineUpdate);
                    netWarnDO.setClearFlag(0);
                    netWarnDO.setCreatTime(new Date());
                    netWarnDO.setWarnId(warnId);
                    // 在线时长
                    if (warnDOMap.containsKey(key)) {
                        String shijian = shijian(warnDOMap, key, offlineUpdate);
                        if (shijian != null) {
                            netWarnDO.setOnlineDuration(shijian);
                        }
                    }
                    machineNetWarnDOS.add(netWarnDO);
                }
            });
        }
        if (!machineNetWarnDOS.isEmpty()) {
            machineNetWarnService.insertAll(machineNetWarnDOS);
        }
    }


    public String shijian(Map<String, MachineNetWarnDO> warnDOMap, String key, String offlineUpdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        MachineNetWarnDO machineNetWarnDO = warnDOMap.get(key);
        String recoverTime = machineNetWarnDO.getRecoverTime();
        try {
            // 恢复时间
            Date date1 = sdf.parse(recoverTime);
            long recover = date1.getTime() / 1000;
            // 离线时间
            Date date2 = sdf.parse(offlineUpdate);
            long offline = date2.getTime() / 1000;
            BigDecimal subtract = new BigDecimal(offline).subtract(new BigDecimal(recover));
            String miao = subtract.toString();
            return String.valueOf(miao);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
